package com.example.jmeter.util;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

import com.example.jmeter.entity.HTTPSampler;
import com.example.jmeter.entity.TestPlan;
import com.example.jmeter.entity.ThreadGroup;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.Data;

/**
 * @program: spring-agent
 * @description:
 * @author: zt22328 赵儒定
 * @create: 2023-04-19 14:12
 **/
public class FreeMarkerUtil {

    /**
     *
     * 通过模板文件生成字符串数据
     *
     * @author StoneLi
     *
     * @param templatePath
     * flt文件路径 src/template
     * @param ftlName
     * flt文件名
     * @param model
     * 数据集合
     * @return
     */
    public static String ftlTempGenerate(String templatePath, String ftlName, Object model) {
        try {
            Configuration cfg = new Configuration();
            cfg.setClassForTemplateLoading(FreeMarkerUtil.class, templatePath);
            Template template = cfg.getTemplate(ftlName, "UTF-8");
            template.setEncoding("UTF-8");
            try (StringWriter out = new StringWriter()) {
                template.process(model, out);
                out.flush();
                return out.getBuffer().toString();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 生成到文件
     * @param templatePath
     * @param ftlName
     * @param model
     * @param destFile
     */
    public static void ftlTempGenerate(String templatePath, String ftlName, Object model, String destFile) {
        try {
            File file = new File(destFile);
            if (!file.exists()) {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                file.createNewFile();
            }
            try (Writer writer = new FileWriter(file)) {
                writer.write(ftlTempGenerate(templatePath, ftlName, model));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 字符串模板生成字符串数据
     *
     * @param template 字符串模板
     * @param model 数据
     * @return 解析后内容
     */
    public static String strTempGenerate(String template, Object model){
        try{
            Configuration cfg = new Configuration();
            StringWriter out = new StringWriter();
            new Template("template", new StringReader(template), cfg)
                    .process(model, out);
            return out.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
//        System.out.println(strTempGenerate("test ${name} !", new HashMap<String, Object>() {{
//            put("name", "zhangs");
//        }}));
        TestPlan testPlan = new TestPlan("测试计划");
        testPlan.setComments("demo测试");

        ThreadGroup threadGroup1 = new ThreadGroup("分组1", 10, 10);

        threadGroup1.addHttpSampler(new HTTPSampler("百度", "www.baidu.com"), new HTTPSampler("MVN", "mvnrepository.com"));

        ThreadGroup threadGroup2 = new ThreadGroup("分组2", 20, 20);

        threadGroup2.addHttpSampler(new HTTPSampler("百度", "www.baidu.com"), new HTTPSampler("MVN", "mvnrepository.com"));

        testPlan.addThreadGroup(threadGroup1, threadGroup2);


        ftlTempGenerate("/template",
                "http.ftl",
                testPlan,
                FreeMarkerUtil.class.getResource("/").getPath() + File.separator + "http.jmx");
//        System.out.println();
    }

}
